<?php

/***  DOCUMENTATION LAYER

Last Update: May 2007
	
DESCRIPTION
	test Greqo Blogger post and delete operations
	
NOTES
	3 sections to edit below:
		1. Meta Data
		2. Procedure Block
		3. Function
	
***/

/* -- EDIT THIS SECTION ----------------------------------------------------- */
// Some Meta Data
$page_title = 'Greqo Blogger';
$test_name = 'Post / Delete';
$run_procedure = 1;
$run_function = 0;
$num_cycles = 10;		// num of cycles to run function

// Include Files
require_once('../blogger.class.php');

/* -- END SECTION ----------------------------------------------------------- */



/* -- NO EDIT --------------------------------------------------------------- */
// Timer
$_tx0 = microtime();
// HEADER
$buffer = "<h1 style='margin:8px 0 0;'>$page_title</h1>";
$buffer .= "<h4 style='margin:0 0 16px;'>$test_name</h4>";	// put script name here
/* -- END SECTION ----------------------------------------------------------- */



/* -- EDIT THIS SECTION ----------------------------------------------------- */

// create Greqo Blogger Object

	require_once('login.inc.php');
	$GreqoBlogger = new GreqoBlogger($debug=0);
	$GreqoBlogger->email = $_GRQ['email'];
	$GreqoBlogger->password = $_GRQ['password'];
	$GreqoBlogger->blog_id = 0;		/* if not sure, login and right click blog link on dashboard -- it will be embedded there */

	// POST values
	if ( !empty($_POST) )
	{
		if ( $GreqoBlogger->debug ) $GreqoBlogger->print_d($GreqoBlogger->print_r($_POST), 'indigo');
	}

// post blog
if ( $_POST['post_entry'] )
{
	// attempt to post
	$body = ( get_magic_quotes_gpc() ) ? stripslashes($_POST['post_body']) : $_POST['post_body'];
	$title = ( get_magic_quotes_gpc() ) ? stripslashes($_POST['post_title']) : $_POST['post_title'];
	$blog_id = $_POST['blog_id'];
	if ( $POST_DATA = $GreqoBlogger->post_entry($body, $title, $blog_id) )
	{
		$dump = print_r($POST_DATA,1);
		$buffer .= "<pre style='color:green;'><b>post succeeded</b> \n $dump</pre>";
		$_TRIGGER['delete_form'] = 1;
	}
	else
	{
		$buffer .= '<p style="color:red;">post failed</p>';
	}
	
	// delete form
	if ( $_TRIGGER['delete_form'] )
	{
		$_HTML['post_form'] = <<<HTML

<form method="post" action="{$_SERVER['PHP_SELF']}">
<input type="submit" name="delete_entry" value="delete this entry" />
<input type="hidden" name="entry_id" value="{$POST_DATA['id']}" />
<input type="hidden" name="blog_id" value="{$blog_id}" />
</form>

HTML;
		
		$buffer .= $_HTML['post_form'];
	}
}

// delete blog
if ( $_POST['delete_entry'] )
{
	$entry_id = $_POST['entry_id'];
	$blog_id = $_POST['blog_id'];
	$buffer .= "<h4 style='margin:0; color:blue;'>attempting to delete entry #$entry_id</h4>";
	
	if ( $GreqoBlogger->delete_entry($entry_id, $blog_id) )
	{
		$buffer .= "<pre style='color:green;'><b>post #$entry_id deleted</b></pre>";
	}
	else
	{
		$buffer .= "<pre style='color:red;'><b>unable to delete post #$entry_id</b></pre>";
	}
	
	$buffer .= "<a href='{$_SERVER['PHP_SELF']}'>click here to start over</a>";
}

// create post form
if ( empty($_POST) ) 
{
	// get blog list
	if ( $BLOG_LIST = $GreqoBlogger->request_blog_list() )
	{
		#print_r($BLOG_LIST);
		$list_html = '';
		$is_first = 0;
		foreach ( $BLOG_LIST['ITEM'] as $_BLOG_DATA ) 
		{
			// get blog id num
			$_cue = '.blog-';
			$blog_id = substr($_BLOG_DATA['id'], strpos($_BLOG_DATA['id'], $_cue) + strlen($_cue));
			$blog_title = $_BLOG_DATA['title'];
		
			$checked = '';
			if ( !$is_first )
			{
				$checked = ' checked ';
				$is_first = 1;
			}
			
			$list_html .= "<input type=\"radio\" name=\"blog_id\" value=\"{$blog_id}\"{$checked}/> $blog_title<br />";
		} 
		
		$debug_note = ( $GreqoBlogger->debug ) ? '' : '<p>set $GreqoBlogger->debug to 1 for verbose output</p>';
		
		$_HTML['blog_list'] = <<<HTML
		
$debug_note

<div class="radio_group">
<h3>choose the blog you wish to post to</h3>
$list_html
</div>

HTML;
	}
	else
	{
		$_HTML['blog_list'] = 'unable to get blog list';
	}

	$_HTML['post_form'] = <<<HTML

<form method="post" action="{$_SERVER['PHP_SELF']}">

<p>{$_HTML['blog_list']}</p>

<label>title:</label> <input type="text" name="post_title" value="greqo test" size="24" /><br />
<label>body:</label><br />
<textarea name="post_body" rows="6" cols="40">
This is a test of the Greqo Blogger class.

For more info, <a href="http://code.google.com/p/greqo/">click here</a>
</textarea>
<br />
<input type="submit" name="post_entry" value="post to blog" />
</form>

HTML;

	$buffer .= $_HTML['post_form'];	
}

/* -- END SECTION ----------------------------------------------------------- */



/* -- EDIT THIS SECTION ----------------------------------------------------- */
// Function Test
if ( $run_function )
{

// Add your function here
function function_name() 
{
// *** DATA

// *** MANIPULATE

// *** RETURN

} // end function


	// run function
	$buffer .= "<h2 style='margin:0; color:green;'>Function Test</h2>";	
	while ( $num_cycles )
	{
		// $result = function_name();
		$buffer .= "<h6 style='margin:0; color:#ccc;'>test #{$run} complete</h6>";
		$num_cycles--;
	}
}

/* -- END SECTION ----------------------------------------------------------- */





/* -- NO EDITING NEEDED BELOW THIS LINE-------------------------------------- */
// output page

// Footer
$_txn = microtime();
$stx = number_format(((substr($_txn,0,9)) + (substr($_txn,-10)) - (substr($_tx0,0,9)) - (substr($_tx0,-10))),4); 
$footer = <<<HTML
<h4 style="margin:0; position:absolute; top:6px; right:6px; font-family:monospace; color:#36f; padding:4px; border:1px solid #36f; font-weight:normal; background:#f3f6ff;">
script completed in $stx s
</h4>
HTML;
$buffer .= $footer;

// Dump Html
$html = <<<HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<title>PHP Test: $page_title</title>
</head>
<body>
$buffer
</body>
</html>
HTML;

echo $html;

?>
